home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 December / PCWorld_2007-12_cd.bin / v cisle / steadystate / SteadyState_Setup_ENU.exe / autologon.wsf < prev    next >
Encoding:
Extensible Markup Language  |  2007-02-12  |  8.9 KB  |  201 lines

  1. <?xml version="1.0" ?>
  2. <package>
  3.     <comment>
  4. ' ***
  5. ' *** ------------------------------------------------------------------------------
  6. ' *** Filename:     AutoLogon.wsf
  7. ' *** ------------------------------------------------------------------------------
  8. ' *** Description:  Command-line tool to Autologon Windows XP with a specific account 
  9. ' *** ------------------------------------------------------------------------------
  10. ' *** Version:      1.0
  11. ' *** Notes:        
  12. ' *** ------------------------------------------------------------------------------
  13. ' *** Copyright (C) Microsoft Corporation 2005, All Rights Reserved
  14. ' *** ------------------------------------------------------------------------------
  15. ' ***
  16.     </comment>
  17.     <job>
  18. <runtime>
  19.             <description>AutoLogon Tool</description>
  20.             <named name="enable" required="false" many="false" helpstring="Enable account to log on to Windows automatically" />
  21.             <named name="disable" required="false" many="false" helpstring="Disable automatic logon to Windows" />
  22.             <usage>
  23. AutoLogon Tool Usage Instructions
  24. ---------------------------------
  25.  
  26. Options:
  27.     Enable  - Enable account to log on to Windows automatically.
  28.     Disable - Disable automatic logon to Windows.
  29.  
  30. Example: AutoLogon.wsf /Enable username password
  31. Example: AutoLogon.wsf /Disable
  32. </usage>
  33.         </runtime>
  34.  
  35.         <resource id="CScriptMessage">Restarting script in command-line mode. Run CmdOn.BAT to set command-line mode as the default mode.</resource>
  36.         <resource id="CScriptTitle">Windows SteadyState: Windows Script Mode Detected</resource>
  37.  
  38. <?job error="True" debug="False" ?>
  39.  
  40. <script language="VBScript">
  41. <![CDATA[
  42.  
  43. ' ~~~ 
  44. ' ~~~ Force variables to be declared 
  45. ' ~~~ 
  46. Option Explicit
  47.  
  48. On Error Resume Next
  49.  
  50. ' ~~~ 
  51. ' ~~~ Declare global variables
  52. ' ~~~ 
  53. Dim sUser, sPassword
  54. Dim oWMIService, oNetwork, oWmiReg
  55. Dim HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS
  56.  
  57. HKEY_CURRENT_USER = &H80000001
  58. HKEY_LOCAL_MACHINE = &H80000002
  59. HKEY_USERS = &H80000003
  60.  
  61. Set oNetwork = CreateObject("Wscript.Network")
  62. Set oWMIService = GetObject("winmgmts:" _
  63.                     & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
  64. Set oWmiReg = GetObject("winmgmts:" _
  65.                     & "{impersonationLevel=impersonate}!\\" & "." & "\root\default:StdRegProv")
  66.     
  67. If WScript.Arguments.Named.Exists("enable") Then
  68.     sUser = Wscript.Arguments(1)
  69.     If sUser = "" Then
  70.         Wscript.Echo "Error: AutoLogon.wsf /Enable - requires an account"
  71.         Wscript.Arguments.ShowUsage
  72.     ElseIf IsValidUser(sUser) Then
  73.             sPassword = ""
  74.             sPassword = Wscript.Arguments(2)
  75.             RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", 1, "REG_SZ"
  76.             RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", sUser, "REG_SZ"
  77.             RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", sPassword, "REG_SZ"
  78.             RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", oNetwork.ComputerName, "REG_SZ"
  79.             RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon", 1, "REG_DWORD"
  80.             Wscript.Echo "Enabled the " & sUser & " account with password " & Chr(34) & sPassword & Chr(34) & " to Auto-Logon to Windows."
  81.             Wscript.Echo "To logon as a different user, press and hold the Shift key during logoff or while restarting the computer."
  82.     Else
  83.             Wscript.Echo "Error: AutoLogon.wsf /Enable - Please enter a valid user account"
  84.             Wscript.Arguments.ShowUsage
  85.     
  86.     End If
  87. ElseIf WScript.Arguments.Named.Exists("disable") Then
  88.     RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", 0, "REG_SZ"
  89.     RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", "", "REG_SZ"
  90.     RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", "", "REG_SZ"
  91.     RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", "", "REG_SZ"
  92.     RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon", 0, "REG_DWORD"
  93.     Wscript.Echo "Disabled Auto-Logon to Windows."
  94. Else
  95.     Wscript.Arguments.ShowUsage
  96. End If
  97.  
  98. ' ***
  99. ' *** --------------------------------------------------------------------------------
  100. ' *** Name:     IsValidUser(sUser)
  101. ' *** --------------------------------------------------------------------------------
  102. ' *** Purpose:      Checks whether the user is valid 
  103. ' *** --------------------------------------------------------------------------------
  104. ' *** 
  105. Function IsValidUser(sUser)
  106.         Dim oAccount
  107.         
  108.         IsValidUser = True
  109.         
  110.         ' ~~~ Turn on error handling
  111.         On Error Resume Next
  112.  
  113.         ' ~~~ Create wmi object & get sid
  114.         Set oAccount = oWMIService.Get("Win32_UserAccount.Name='" & sUser & "',Domain='" & oNetwork.ComputerName & "'")
  115.         If oAccount.SID = "" Then IsValidUser = False     
  116.         
  117.         Set oAccount = Nothing    
  118. End Function
  119.  
  120. ' ***
  121. ' *** ------------------------------------------------------------------------------
  122. ' *** Name:         RegWrite(sRegKey, sValue, sType)
  123. ' *** ------------------------------------------------------------------------------
  124. ' *** Purpose:      Writes a registry key using WMI
  125. ' *** ------------------------------------------------------------------------------
  126. ' ***
  127. Function RegWrite(sRegKey, sValue, sType)
  128.  
  129.     Dim sRegHive, iCharStart, sRegKeyPath, sRegKeyName, iCharEnd, iReturn 
  130.     
  131.     ' ~~~ Turn on error 'handling'
  132.     On Error Resume Next
  133.     
  134.     iReturn = 0
  135.  
  136.     iCharStart = InStr(sRegKey, "\")
  137.     ' ~~~ Get HKLM\HKCU\HKU
  138.     sRegHive = Left( sRegKey , iCharStart-1 )
  139.     
  140.     iCharEnd = InStrRev( sRegKey, "\")
  141.     ' ~~~ Store the reg key for which the value has to be updated
  142.     sRegKeyName = Right( sRegKey, Len( sRegKey) - iCharEnd )
  143.     
  144.     ' ~~~ Store the registry path of the key 
  145.     sRegKeyPath = Mid( sRegKey , iCharStart+1 , iCharEnd - iCharStart-1  ) 
  146.     
  147.     Select Case sType
  148.         Case "REG_SZ"
  149.             If sRegHive = "HKLM" OR sRegHive = "HKEY_LOCAL_MACHINE" Then
  150.                 iReturn = oWmiReg.SetStringValue( HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , sValue )
  151.                 If iReturn <> 0 Then 
  152.                     oWmiReg.CreateKey HKEY_LOCAL_MACHINE, sRegKeyPath 
  153.                     oWmiReg.SetStringValue HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , sValue
  154.                 End If
  155.             ElseIf sRegHive = "HKCU" OR sRegHive = "HKEY_CURRENT_USER" Then
  156.                 iReturn = oWmiReg.SetStringValue( HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , sValue)
  157.                 If iReturn <> 0 Then
  158.                     oWmiReg.CreateKey HKEY_CURRENT_USER, sRegKeyPath 
  159.                     oWmiReg.SetStringValue HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , sValue
  160.                 End If
  161.             ElseIf sRegHive = "HKU" OR sRegHive = "HKEY_USERS" Then
  162.                 iReturn = oWmiReg.SetStringValue( HKEY_USERS, sRegKeyPath , sRegKeyName , sValue )
  163.                 If iReturn <> 0 Then
  164.                     oWmiReg.CreateKey HKEY_USERS, sRegKeyPath 
  165.                     oWmiReg.SetStringValue HKEY_USERS, sRegKeyPath , sRegKeyName , sValue
  166.                 End If
  167.             End If
  168.         Case "REG_DWORD" 
  169.             If Not(IsNumeric(sValue)) Then sValue="0"
  170.             
  171.             If sRegHive = "HKLM" OR sRegHive = "HKEY_LOCAL_MACHINE" Then
  172.                 iReturn = oWmiReg.SetDWORDValue( HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , Int(sValue) )
  173.                 If iReturn <> 0 Then
  174.                     oWmiReg.CreateKey HKEY_LOCAL_MACHINE, sRegKeyPath 
  175.                     oWmiReg.SetDWORDValue  HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , Int(sValue)
  176.                 End If
  177.             ElseIf sRegHive = "HKCU" OR sRegHive = "HKEY_CURRENT_USER" Then
  178.                 iReturn =   oWmiReg.SetDWORDValue( HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , Int(sValue) )
  179.                 If iReturn <> 0 Then
  180.                     oWmiReg.CreateKey HKEY_CURRENT_USER, sRegKeyPath 
  181.                     oWmiReg.SetDWORDValue  HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , Int(sValue)
  182.                 End If
  183.             ElseIf sRegHive = "HKU" OR sRegHive = "HKEY_USERS" Then
  184.                 iReturn = oWmiReg.SetDWORDValue( HKEY_USERS, sRegKeyPath , sRegKeyName , Int(sValue))
  185.                 If iReturn <> 0 Then
  186.                     oWmiReg.CreateKey HKEY_USERS, sRegKeyPath 
  187.                     oWmiReg.SetDWORDValue  HKEY_USERS, sRegKeyPath , sRegKeyName , Int(sValue)
  188.                 End If
  189.             End If
  190.     End Select  
  191. End Function
  192.  
  193. ' ~~~ Destroy objects
  194. Set oNetwork = Nothing  
  195. Set oWMIService = Nothing
  196. Set oWmiReg = Nothing
  197. ]]>
  198. </script>
  199. </job>
  200. </package>
  201.